package org.obs.core.hibernate.operations;

import org.hibernate.Query;
import org.hibernate.Session;
import org.obs.core.HibernateOperation;
import org.obs.domain.UserRequest;
import org.obs.form.CreateUserForm;

public class CreateUserByRequestOperation implements HibernateOperation <Boolean> {

	private String name;

	private String value;
	
	
	public CreateUserByRequestOperation(String n, String v) {
		this.name = n;
		this.value = v;
	}

	public Boolean execute(Session session) {
		String queryString = "from UserRequest where n = :n and v = :v";
		Query query = session.createQuery(queryString);
		query.setString("n", name);
		query.setString("v", value);
		
		UserRequest request = (UserRequest) query.uniqueResult();
		if (request == null) {
			return Boolean.FALSE;
		}
		
		CreateUserForm form = new CreateUserForm();
		form.setEmail(request.getEmail());
		form.setNick(request.getNick());
		form.setPassword(request.getPassword());
		
		new CreateUserOperation(form).execute(session);
		
		return Boolean.TRUE;
	}

}
